<?php
class ForeignPoliceExport_Controller extends AdminController {

	private $data;
	
	public function __construct() {
		parent::__construct();
		$sessionData = $this->getSessionData();
		$this->data = $sessionData->getForeignPoliceExportData();
	}
	
	public function init() {
		$sessionData = $this->getSessionData();
		$this->data = new ForeignPoliceExportData();
		$sessionData->setForeignPoliceExportData($this->data);
		$this->redirect('/admin/foreignPoliceExport/form');
	}

	public function form() {
		// Add person options
		$this->viewData['personOptions'] = $this->dao->queryOptions(
				'SELECT id, first_name, last_name, birth_date FROM person ' .
				'ORDER BY last_name ASC, first_name ASC, birth_date ASC',
				NULL, array(' ', '()'));
		
		$this->viewData['personList'] = $this->data->personList;
		
		$this->renderMySubview('foreignPoliceExportForm');
	}

	public function export() {
		$headers = array(
			'birth_date' => text::get('person-birth_date'),
			'last_name' => text::get('person-last_name'),
			'first_name' => text::get('person-first_name'),
			'other_names' => text::get('person-other_names'),
			'idcard_number' => text::get('person-idcard_number'),
			'idcard_type_name' => text::get('person-idcard_type'),
			'citizenship_name' => text::get('person-citizenship_code'),
			'street' => text::get('person-street'),
			'city' => text::get('person-city'),
			'postal_code' => text::get('person-postal_code'),
			'country_name' => text::get('person-country_code'),
		);
		$columnKeys = array(
				new ColumnDescriptor('birth_date', ColumnDescriptor::$TYPE_DATE),
				'last_name',
				'first_name',
				'other_names',
				new ColumnDescriptor('idcard_number', ColumnDescriptor::$TYPE_TEXT),
				'idcard_type_name',
				'citizenship_name',
				'street',
				'city',
				new ColumnDescriptor('postal_code', ColumnDescriptor::$TYPE_TEXT),
				'country_name');
		$records = array_merge(array($headers), $this->data->personList);
		// debug::dump($records);
		$tmpExcelFileName = excel::simpleReport($records, $columnKeys, TRUE);
        download::force($tmpExcelFileName, NULL, 'Export pro policii - ' . date('Y-m-d') . '.xlsx');
	}
	
	public function addPerson() {
		$personId = $_POST['person_id'];
		
		if (! valid::isId($personId)) {
			$this->msg->error('error-invalidId');
			$this->redirect('/admin/foreignPoliceExport');
		}
		
		$person = $this->dao->getPerson($personId);
		if (! $person) {
			$this->msg->error('error-recordNotFound');
			$this->redirect('/admin/foreignPoliceExport');
		}
		
		if ($person['idcard_type']) {
			$person['idcard_type_name'] = text::get('person-idcard_type-' . $person['idcard_type']);
		}
		if ($person['citizenship_code']) {
			$country = $this->dao->getByCode('country', $person['citizenship_code']);
			$person['citizenship_name'] = $country['name'];
		}
		if ($person['country_code']) {
			$country = $this->dao->getByCode('country', $person['country_code']);
			$person['country_name'] = $country['name'];
		}
		
		$this->data->personList[$person['id']] = $person;
		// $this->redirect('/admin/foreignPoliceExport/form');
		
		$this->viewData['personList'] = $this->data->personList;
		$this->renderForAjax('admin/foreignPoliceExport/foreignPoliceExportList', 'exportList');
	}
	
	public function removePerson() {
		$id = $_GET['person_id'];
// 		for ($i = 0; $i < count($this->data->personList); $i++) {
// 			if ($id == $this->data->personList[$i]['id']) {
// 				unset($this->data->personList[$i]);
// 			}
// 			else {
// 				echo $this->data->personList[$i]['id'] . ', ';
// 			}
// 		}
		unset($this->data->personList[$id]);
		$this->redirect('/admin/foreignPoliceExport/form');
	}
}
